<?php
/**
 *
 * Author: Lyu
 * Date: 2023/7/24
 * Time: 10:23
 */

namespace App\Service\Core\Admin\Menu;

use App\Model\Core\Admin\AdminMenuModel;
use App\Service\Core\CoreBaseService;
use App\Traits\ModelStoreTrait;


/**
 * @method $this setName($val)
 * @method $this setParentId($val)
 * @method $this setPath($val)
 * @method $this setMeta($val)
 * @method $this setComponent($val)
 * @method $this setRedirect($val)
 * @method $this setActive($val)
 * {"title":"其他", "icon":"el-icon-more", "type":"menu", "color":"#C52B2B", "hidden":true, "hiddenBreadcrumb":true, "fullpage":true, "tag":"标签"}
 * @package App\Service\Core\Sys\Menu
 */
class AdminMenuStoreService extends CoreBaseService
{
    use ModelStoreTrait;

    private array $required_param = ['name', 'meta'];

    public function store($id = 0): array
    {
        $this->check_required_param($this->required_param);
        if (!empty($this->errors)) {
            return alert_info(1, $this->errors[0], $this->errors);
        }
        $name = $this->getParam('name');
        $meta = $this->getParam('meta');
        $parent_id = $this->getParam('parent_id');
        $path = $this->getParam('path');
        $component = $this->getParam('component');
        $redirect = $this->getParam('redirect');
        $active = $this->getParam('active');

        if (empty($meta['title']) || empty($meta['type'])) {
            return alert_info(1, '菜单类型或显示名称不存在');
        }
        $meta['icon'] = $meta['icon'] ?? '';
        $meta['color'] = $meta['color'] ?? '';
        $meta['hidden'] = $meta['hidden'] ?? false;
        $meta['hiddenBreadcrumb'] = $meta['hiddenBreadcrumb'] ?? false;
        $meta['fullpage'] = $meta['fullpage'] ?? '';
        $meta['tag'] = $meta['tag'] ?? '';

        $data = [
            'name' => $name,
            'meta' => json_encode($meta, JSON_UNESCAPED_UNICODE),
            'parent_id' => $parent_id,
            'path' => $path,
            'component' => $component,
            'redirect' => $redirect,
            'active' => $active,
        ];
        if ($id) {
            $menu_info = AdminMenuService::getInstance()->getOne($id);
            if (empty($menu_info)) {
                return alert_info(1, '编辑菜单不存在');
            }
            $change_data = easy_check_change_data($menu_info, $data);
            if (empty($change_data)) {
                return alert_info(1, '信息没有变化');
            }
            $data['updated_at'] = date('Y-m-d H:i:s');
            $data['updated_by'] = $this->operate_by;
            $res = AdminMenuModel::getInstance()->updateOne($id, $data);
        } else {
            $data['created_at'] = date('Y-m-d H:i:s');
            $data['created_by'] = $this->operate_by;
            $data['updated_at'] = date('Y-m-d H:i:s');
            $data['updated_by'] = $this->operate_by;
            $res = AdminMenuModel::getInstance()->addOne($data);
            $id = $res;
        }
        if ($res === false) {
            return alert_info(1, '操作失败：数据库繁忙');
        }
        return alert_info(0, '操作成功', ['id' => $id]);
    }

}
